home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / text / misc / ctutor2.txt.pp / ctutor2.txt
Text File  |  1994-11-17  |  13KB  |  299 lines

  1. -surgery - Operations in "C"
  2. -----------------------------
  3.  
  4.     The C language was designed as a "middle level" language, that is a
  5. language which lies somewhere between a high level language like BASIC and a
  6. low level language like Assembler. This results in a language which is
  7. harder to program in than basic, yet has features traditionally found only
  8. in low level languages. Translation - Power, efficiency, speed etc. When
  9. examining the operators in C it becomes very apparent that C is capable of
  10. somethings that just don't exist in other languages.
  11.  
  12.     Below is a table showing "C Precendence and Associativity". When
  13. programming in C this can be of great help. All of the C operators in the
  14. chart are explained below.
  15.  
  16. ....Cut Here....Cut Here....Cut Here....Cut Here....Cut Here....
  17.  
  18. Category    Assoc.    Operators
  19.  
  20. primary        ltor    ()  []  .  ->
  21.  
  22. secondary    rtol    *  &  ++  --  ~  !  -  sizeof()  (cast)
  23.  
  24. binary        ltor    *  /  %
  25.             +  -
  26.             << >>
  27.             <  >  <= >=
  28.             == !=
  29.             &
  30.             ^
  31.             |
  32.             &&
  33.             ||
  34. ternary        rtol    ?:
  35.             =  += -= *= /= %= >>= <<= &= |=
  36. comma        ltor    ,
  37.  
  38. Notes:    Operators on same line have same precendence.
  39.     The operators ! < > <= >= == != && || are logical
  40.      operators.
  41.  
  42. ....Cut Here....Cut Here....Cut Here....Cut Here....Cut Here....
  43.  
  44.     So now we have a list of the operators available in C. All that remains
  45. is to go through the list one by one and explain each ones use. Note: It
  46. must be assumed that a basic knowledge of C data types exists or this
  47. disscusion will fall short very quickly.    Starting from the top, we can knock of the first level pretty quickly.
  48. The () operators are used just as in regular math to ESTABLISH precedence.
  49. That is to force the order of operations into a specific order. Items within
  50. () are evaluated first. Next the square brakets [] are used in C as in
  51. pascal and basic, to reference an array, so the fourth element in the array,
  52.  
  53.     char letters[10];
  54.  
  55.     We would reference letters[3] (note, ALL C arrays start at element 0, so
  56. the array letters would have 10 elements numbered 0 through 9). So far so
  57. simple. Next the . operator, this is refered to as the member operator and
  58. is used to specify a specific memeber of a structure. For example using the
  59. structure,
  60.  
  61.     struct {
  62.         char number[13];
  63.         int  unit;
  64.         char street[25];
  65.         } PhoneBook;
  66.  
  67.     If we wanted the unit number of the record, we would reference it by
  68. 'PhoneBook.unit' similarly any of the other members could be referenced. In
  69. C however structures are more often referenced by pointers. That is
  70. 'PhoneBook' in the above example was in fact declared as '*PhoneBook' then
  71. in order to reference the member would be '(*PhoneBook).unit'. This is a
  72. rather awkward thing to write, especially as it is such a common occourence
  73. in C prpgramming. To alleviate this problem C provides the "Pointer"
  74. operator. The above '(*PhoneBook).unit' can now be replaced by 'PhoneBook-
  75. >unit' which translates to "PhoneBook pointing to the member unit".
  76.  
  77.     That end the discussion of the primary operators. The next level of
  78. operators are ones of the unary type. First however a quick review of
  79. pointers.
  80.  
  81.     A pointer is a variable which "points" to a value or group of values.
  82. That is rather than storing the actual value in the variable, the variable
  83. contains the memory location where the value can be found. This concept is
  84. crucial to C for several very important reasons, and because of this several
  85. C operators exist specifically to allow pointers to function. The first of
  86. these has already been discussed, that is the '->' operator. The next is the
  87. * operator. The * operator is used to declare a variable as a pointer as in,
  88.  
  89.     char *ptr;
  90.  
  91.     which declares a varibale 'ptr' which points to a character value. Also
  92. the * operator tells C to take the value pointed to by a pointer, so if the
  93. variable 'x' is declared as type char, then
  94.  
  95.     x = *ptr;
  96.  
  97.     will take the value pointed to by the pointer variable 'ptr', and stores
  98. it in the variable 'x'.    The next operator, &, does the exact opposite, that is it says return a
  99. pointer to the given variable, so the line
  100.  
  101.     ptr = &x;
  102.  
  103.     would make the varibale 'ptr' point to the variable x. It is important
  104. to realize the implications of this. So the following example is shown.
  105.  
  106.     C Code            x            *ptr
  107.  
  108.     int x=0;        0            ????
  109.     int *ptr;        0            ????
  110.     ptr = &x;        0              0
  111.     x = 5;            5              5
  112.     *ptr = 8;        8              8
  113.     x = x+2            10              10
  114.  
  115.     The important idea here is that changes to one effect the other, note
  116. that the ???? is because until the pointer is actually assigned, we have no
  117. idea what it points to. For this reason the idea of a NULL pointer exists.
  118. This is a pointer which points to nothing.
  119.  
  120.     The next operators are the ++ and -- operators, they stand for increment
  121. and decrement. These operators will increase or decrease the value of a
  122. variable by 1 unit. They may be placed before or after the variable in
  123. question as,
  124.  
  125.     x++;    /* post increment */
  126.     ++x;    /* pre  increment */
  127.  
  128.     The pre and post forms of these operators are important in that they
  129. determine how a function will behave. For instance in,
  130.  
  131.     b = 5;
  132.     a = b++; 
  133.  
  134.     'a' will recieve the value 5, while 'b' will have the value 6. This
  135. is different from the code fragent,
  136.  
  137.     b = 5;
  138.     a = ++b;
  139.  
  140.     where both 'a' and 'b' have the value 6. The next operators of
  141. importance are the - and ~ operators. The returns the "two's complement" or
  142. negative value, while the second returns the "one's complement" or binary
  143. negation. The effect of the negative operator is simple, 'a = -b' would
  144. assign the negative of 'b' to 'a', however 'a = ~b' has a somewhat different
  145. effect. If b = 5, and 'b' is a regular short integer, then the value of 'b'
  146. in binary is, 0000 0101, and the value of ~b is 1111 1010. The exact binary
  147. inverse of 'b'.    There are two more operators of the unary type, the first is sizeof().
  148. This returns the size, in bytes, of a data type. The result is evaluated at
  149. compile time, and replaced by a literal, so sizeof(char) is 1 byte,
  150. sizeof(SHORT) is 2, etc. This is especially useful when allocating memory
  151. for a structure, ie
  152.  
  153.     struct Window *win;
  154.  
  155.     win = (struct Window *) malloc(sizeof(struct Window));
  156.  
  157.     Ignoring most of the line, if a window structure requires 53 bytes, the
  158. above line will return a pointer to an area of memory 53 bytes long, big
  159. enough to store the information required by a window structure. The last
  160. unary operator is the ! or 'not' operator. Quite simply it takes a true (ie
  161. non-zero) result and makes it false (ie 0) or the opposite, so if a variable
  162. 'a' needs to be tested to see if it is zero, then the test 'if (!a)' will be
  163. true if a is 0 and false otherwise. This is true also for pointers, a test
  164. 'if (!ptr)' will be true if the pointer evaluates to NULL, and false
  165. otherwise.
  166.  
  167.     The stuff in brackets before malloc(...) is an example of a cast
  168. operator. What it does is force C to convert the results of the value on the
  169. right into a data type which corresponds to the cast. In the above example
  170. the cast is of type "pointer to a window structure" the code is forcing C to
  171. convert the pointer being returned by the function "malloc()" to be a Window
  172. pointer. Similarly, if a function varible in a function is floating point,
  173. and it is desired to force that value to integer for the purposes of a given
  174. expression then it can be casted into that type. An example is,
  175.  
  176.     int x,y,z;
  177.     float a,b;
  178.  
  179.     x = 10;
  180.     a = 2.5;
  181.  
  182.     b = x / a;         /* results in b = 4 */
  183.     b = x / (int) a;    /* results in b = 5 */
  184.  
  185.     It is obvious where cast operators could be useful.
  186.  
  187.     The next set of operators are fairly straightforward, and can be handled
  188. quickly. These are the binary operators. The first set of these are the
  189. operators *, /, %, +, and - which perform the functions of multiplication,
  190. division, modulos, addition, and subtraction respectively. So 'a+b' will
  191. re